package com.ruoyi;

import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.nio.charset.Charset;

public class SSOApplication {
    public static void main(String[] args) {
        String url = "http://localhost/login";
        System.out.println(doGet(url, "sdjhsadasdjksadhuweoiwifhwe"));
        System.out.println("已发送");
        //response.setContentType("application/json;charset=UTF-8");
        //response.getWriter().append(JSON.toJSON(R.ok(token,"操作成功")).toString());
//        response.getWriter().append("{\"code\":0,\"msg\":\"登录成功!\",\"data\":\"success\"}");
    }

    public static String doGet(String url, String token) {
        String reponse = null;
        // 1.拿到一个httpclient的对象
        CloseableHttpClient httpClient = HttpClients.createDefault();
        // 2.设置请求方式和请求信息
        // 设置请求格式
        HttpGet httpGet = new HttpGet(url+"?code="+token);
//        httpGet.setHeader("Authorization", token);  //假设值测试是否可取得


        try {
            // 3.执行请求
            CloseableHttpResponse response = httpClient.execute(httpGet);
            // 4.获取返回值--在这测试是，不需要
            //   在这个功能的测试里，不重要
            HttpEntity entity = response.getEntity();
            String result = EntityUtils.toString(entity,"utf-8");
//            reponse = EntityUtils.toString(entity, Charset.forName("utf-8"));
//            System.out.println(reponse);
            // 5.打印
            return result;
        }
        catch (Exception e ) {
            e.printStackTrace();
        }

        return reponse;
    }

}
